def async_wait<%= gasync.nil? ? ("_" + api.out_name) : "" %>(config, result, client, timeout):
    module = config.module
<%
   async = gasync.nil? ? api.async : gasync
   async_op = async.operation
-%>
<% if (!gasync.nil?) || need_build_async_url(api) %>
<%   if async_op.path_parameter -%>
    path_parameters = {
<%     async_op.path_parameter.each do |k, v| -%>
<%=      sprintf("%s\"%s\": [%s],", ' ' * 8, k, index2navigate(v))  %>
<%     end -%>
    }
    data = {
        key: navigate_value(result, path)
        for key, path in path_parameters.items()
    }

    url = build_path(module, "<%= async_op.path -%>", data)
<%   elsif async_op.path.include?("{") -%>
    url = build_path(module, "<%= async_op.path -%>", result)
<%   else -%>
    url = "<%= async_op.path -%>"
<%   end -%>
<% end -%>
<% cl = "client" -%>
<% status_check = async.status_check -%>
<% if status_check -%>

    def _query_status():
        r = None
        try:
            r = <%= cl %>.get(url, timeout=timeout)
        except HwcClientException:
            return None, ""

        try:
            s = navigate_value(r, [<%= index2navigate(status_check.field) %>])
            return r, s
        except Exception:
            return None, ""

    try:
        return wait_to_finish(
            [<%= status_check.complete.compact.map { |x| "\"#{x}\"" }.join(", ") %>],
<%   if status_check.pending.nil? -%>
            None,
<%   else -%>
            [<%= status_check.pending.compact.map { |x| "\"#{x}\"" }.join(", ") %>],
<%   end -%>
            _query_status, timeout)
    except Exception as ex:
        module.fail_json(msg="module(<%= module_name %>): error "
<%   if gasync.nil? -%>
                             "waiting for api(<%= api.name %>) to "
                             "be done, error= %s" % str(ex))
<%   else -%>
                             "waiting to be done, error= %s" % str(ex))
<%   end -%>
<% elsif async.custom_status_check_func -%>

    def _query_status():
        r = None
        try:
            r = <%= cl %>.get(url, timeout=timeout)
        except HwcClientException:
            return None, ""

        if <%= async.custom_status_check_func %>(r):
            return r, "Done"

        return r, "Pending"

    try:
        return wait_to_finish(["Done"], ["Pending"], _query_status, timeout)
    except Exception as ex:
        module.fail_json(msg="module(<%= module_name %>): error "
<%   if gasync.nil? -%>
                             "waiting for api(<%= api.name %>) to "
                             "be done, error= %s" % str(ex))
<%   else -%>
                             "waiting to be done, error= %s" % str(ex))
<%   end -%>
<% else -%>
<%   raise "Must sepecify status check for api: " + (gasync.nil? ? api.name : "global async") -%>
<% end -%>
